package com.haitaoit.chengquan.module.near.adapter;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.haitaoit.chengquan.R;
import com.haitaoit.chengquan.base.BackCall;
import com.haitaoit.chengquan.base.ExitApplication;
import com.haitaoit.chengquan.module.home.activity.maintenance.MaintenanceDetailActivity2;
import com.haitaoit.chengquan.module.home.activity.maintenance.SeachDemandActivity;
import com.haitaoit.chengquan.module.near.model.NearServerModel;
import com.haitaoit.chengquan.utils.AppLog;
import com.haitaoit.chengquan.view.QRatingBar;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/10/24.
 */
public class MaplistAdapter extends BaseAdapter {
    private boolean  isService;
    private Context  context;
    private BackCall backCall;
    private Intent   intent;
    private ArrayList<NearServerModel> list = new ArrayList<NearServerModel>();

    public void setBackCall(BackCall backCall) {
        this.backCall = backCall;
    }

    public MaplistAdapter(Context context) {
        this.context = context;
    }

    public boolean isService() {
        return isService;
    }

    public void setService(boolean service) {
        isService = service;
    }

    public ArrayList<NearServerModel> getDataList() {
        return list;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_maplist, null);
            holder = new ViewHolder();
            ViewUtils.inject(holder, convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final NearServerModel nearServerModel = list.get(position);
        holder.title.setText(nearServerModel.getName());

        AppLog.D("nearServerModel.getReference_price():" + nearServerModel.getReference_price());
        AppLog.D("nearServerModel.getScore():" + nearServerModel.getScore());
        if (isService) {
            intent = new Intent(context, MaintenanceDetailActivity2.class);

            holder.summary.setText("信誉值：");
            holder.eva_rat.setRating(Float.valueOf(nearServerModel.getScore()));
            holder.eva_rat.setVisibility(View.VISIBLE);

        } else {
            intent = new Intent(context, SeachDemandActivity.class);
            holder.summary.setText("参考价格：¥" + nearServerModel.getReference_price());
            holder.eva_rat.setVisibility(View.GONE);

        }
        holder.distance.setText("距离：" + nearServerModel.getDistance());
        holder.rl_demand_details.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intent.putExtra("id", list.get(position).getId());
                context.startActivity(intent);
            }
        });
        holder.go_here.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isAvilible(context, "com.baidu.BaiduMap")) {//传入指定应用包名
                    //                          intent = Intent.getIntent("intent://map/direction?origin=latlng:34.264642646862,108.95108518068|name:我家&destination=大雁塔&mode=driving®ion=西安&src=yourCompanyName|yourAppName#Intent;scheme=bdapp;package=com.baidu.BaiduMap;end");
                    intent = new Intent();
                       /* intent = Intent.getIntent("intent://map/direction?" +
                                //"origin=latlng:"+"34.264642646862,108.95108518068&" +   //起点  此处不传值默认选择当前位置
                                "destination=latlng:"+location[0]+","+location[1]+"|name:我的目的地"+        //终点
                                "&mode=driving&" +          //导航路线方式
                                "region=北京" +           //
                                "&src=慧医#Intent;scheme=bdapp;package=com.baidu.BaiduMap;end");*/
                    intent.setData(Uri.parse("baidumap://map/walknavi?origin=" + ExitApplication.latitude + "," + ExitApplication.longitude + "&destination=" + nearServerModel.getLatitude() + "," + nearServerModel.getLongitude()));
                    context.startActivity(intent); //启动调用

                } else {//未安装
                    //market为路径，id为包名
                    //显示手机上所有的market商店
                    com.github.tools.ToastUtils.showToast(context, "您尚未安装百度地图");

                }
            }
        });
        //        ImageLoader.getInstance().displayImage(list.get(position).getImg_url(), holder.center_img);
        //        holder.tv_title.setText(list.get(position).getName());
        //        holder.tv_content.setText(list.get(position).getIntroduction());
        //
        //        holder.brand_itemll.setOnClickListener(new View.OnClickListener() {
        //            @Override
        //            public void onClick(View v) {
        //                MaplistAdapter.this.backCall.deal(R.id.brand_itemll, position);
        //            }
        //        });
        return convertView;
    }

    class ViewHolder {
        @ViewInject(R.id.title)
        TextView       title;
        @ViewInject(R.id.summary)
        TextView       summary;
        @ViewInject(R.id.go_here)
        TextView       go_here;
        @ViewInject(R.id.distance)
        TextView       distance;
        @ViewInject(R.id.rl_demand_details)
        RelativeLayout rl_demand_details;
        @ViewInject(R.id.eva_rat)
        QRatingBar     eva_rat;
    }

    public static boolean isAvilible(Context context, String packageName) {
        //获取packagemanager
        final PackageManager packageManager = context.getPackageManager();
        //获取所有已安装程序的包信息
        List<PackageInfo> packageInfos = packageManager.getInstalledPackages(0);
        //用于存储所有已安装程序的包名
        List<String> packageNames = new ArrayList<String>();
        //从pinfo中将包名字逐一取出，压入pName list中
        if (packageInfos != null) {
            for (int i = 0; i < packageInfos.size(); i++) {
                String packName = packageInfos.get(i).packageName;
                packageNames.add(packName);
            }
        }
        //判断packageNames中是否有目标程序的包名，有TRUE，没有FALSE
        return packageNames.contains(packageName);
    }
}
